package com.sos.core.game 
{
	import com.sos.core.Player;
	import com.sos.core.DataObject;
	
	import com.sos.logging.Dumper;
	import com.sos.logging.IDumpable;
	
	/**
	 * ...
	 * @author ultima
	 */
	public class GameListItem extends DataObject implements IDumpable
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON Property names
		public static const 	JSON_PROPERTY_GAME_ID:String				= "id";
		public static const 	JSON_PROPERTY_PLAYER_SIDE:String			= "playerSide";
		public static const 	JSON_PROPERTY_OPPONENT_ID:String			= "opponentID";
		public static const 	JSON_PROPERTY_OPPONENT_NAME:String			= "opponentName";
		public static const 	JSON_PROPERTY_PLAYER_DECK_ID:String			= "deckID";
		public static const 	JSON_PROPERTY_PLAYER_DECK_NAME:String		= "deckName";
		public static const 	JSON_PROPERTY_OPPONENT_DECK_ID:String		= "opponentDeckID";
		public static const 	JSON_PROPERTY_OPPONENT_DECK_NAME:String		= "opponentDeckName";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _side:GameSide;
		private var _opponent:Player;
		private var _playerDeckId:uint;
		private var _playerDeckName:String;
		private var _opponentDeckId:uint;
		private var _opponentDeckName:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameListItem(data:Object = null) 
		{
			// Clear
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get id():uint 
		{
			return _id;
		}
		
		public function set id(value:uint):void 
		{
			_id = value;
		}
		
		public function get side():GameSide 
		{
			return _side;
		}
		
		public function set side(value:GameSide):void 
		{
			_side = value;
		}
		
		public function get opponent():Player 
		{
			return _opponent;
		}
		
		public function set opponent(value:Player):void 
		{
			_opponent = value;
		}
		
		public function get playerDeckId():uint 
		{
			return _playerDeckId;
		}
		
		public function set playerDeckId(value:uint):void 
		{
			_playerDeckId = value;
		}
		
		public function get playerDeckName():String 
		{
			return _playerDeckName;
		}
		
		public function set playerDeckName(value:String):void 
		{
			_playerDeckName = value;
		}
		
		public function get opponentDeckId():uint 
		{
			return _opponentDeckId;
		}
		
		public function set opponentDeckId(value:uint):void 
		{
			_opponentDeckId = value;
		}
		
		public function get opponentDeckName():String 
		{
			return _opponentDeckName;
		}
		
		public function set opponentDeckName(value:String):void 
		{
			_opponentDeckName = value;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			// Required fields
			if (!data.hasOwnProperty(JSON_PROPERTY_GAME_ID))			throwLoadMissingPropError(JSON_PROPERTY_GAME_ID);
			if (!data.hasOwnProperty(JSON_PROPERTY_PLAYER_SIDE))		throwLoadMissingPropError(JSON_PROPERTY_PLAYER_SIDE);
			if (!data.hasOwnProperty(JSON_PROPERTY_OPPONENT_ID))		throwLoadMissingPropError(JSON_PROPERTY_OPPONENT_ID);
			if (!data.hasOwnProperty(JSON_PROPERTY_OPPONENT_NAME))		throwLoadMissingPropError(JSON_PROPERTY_OPPONENT_NAME);
			if (!data.hasOwnProperty(JSON_PROPERTY_PLAYER_DECK_ID))		throwLoadMissingPropError(JSON_PROPERTY_PLAYER_DECK_ID);
			if (!data.hasOwnProperty(JSON_PROPERTY_PLAYER_DECK_NAME))	throwLoadMissingPropError(JSON_PROPERTY_PLAYER_DECK_NAME);
			if (!data.hasOwnProperty(JSON_PROPERTY_OPPONENT_DECK_ID))	throwLoadMissingPropError(JSON_PROPERTY_OPPONENT_DECK_ID);
			if (!data.hasOwnProperty(JSON_PROPERTY_OPPONENT_DECK_NAME))	throwLoadMissingPropError(JSON_PROPERTY_OPPONENT_DECK_NAME);
		}
		
		override protected function fill(data:Object):void
		{
			// Value parsing and casting
			var id:uint					= uint(data[JSON_PROPERTY_GAME_ID]);
			var side:GameSide			= GameSide.getByName(data[JSON_PROPERTY_PLAYER_SIDE]);
			var opponentId:uint			= uint(data[JSON_PROPERTY_OPPONENT_ID]);
			var opponentName:String		= data[JSON_PROPERTY_OPPONENT_NAME];
			var playerDeckId:uint		= uint(data[JSON_PROPERTY_PLAYER_DECK_ID]);
			var playerDeckName:String	= data[JSON_PROPERTY_PLAYER_DECK_NAME];
			var opponentDeckId:uint		= uint(data[JSON_PROPERTY_OPPONENT_DECK_ID]);
			var opponentDeckName:String	= data[JSON_PROPERTY_OPPONENT_DECK_NAME];
			// Fill objects
			_id					= id;
			_side				= side
			_playerDeckId		= playerDeckId;
			_playerDeckName		= playerDeckName;
			_opponent			= new Player({userID: opponentId, username: opponentName});
			_opponentDeckId		= opponentDeckId
			_opponentDeckName	= opponentDeckName;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			_id					= DataObject.INVALID_ID;
			_side				= GameSide.NONE;
			_opponent			= null;
			_playerDeckId		= DataObject.INVALID_ID;
			_playerDeckName		= "";
			_opponentDeckId		= DataObject.INVALID_ID;
			_opponentDeckName	= "";
		}
		
		public function copy(gameListItem:GameListItem):void
		{
			_id 				= gameListItem.id;
			_side				= gameListItem.side;
			_opponent			= gameListItem.opponent;
			_playerDeckId		= gameListItem.playerDeckId;
			_playerDeckName		= gameListItem.playerDeckName;
			_opponentDeckId		= gameListItem.opponentDeckId;
			_opponentDeckName	= gameListItem.opponentDeckName;
		}
		
		public function clone():GameListItem
		{
			var item:GameListItem = new GameListItem();
			item.copy(this);
			return item;
		}
		
		public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}
		
	}

}